package com.study.manage.dal;

import java.util.List;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import com.study.manage.domain.BlogMedia;
import com.study.manage.domain.Media;


public interface BlogMediaRepository
        extends PagingAndSortingRepository<BlogMedia, Long>, JpaSpecificationExecutor<BlogMedia> {

    @Query(value = "select m.url from sa_blog_media as t left join sa_media as m on t.media_id=m.id "
            + "where t.blog_id=:blogId and t.state=0 limit 1", nativeQuery = true)
    String findOneImgUrlByBlogId(@Param("blogId") Long blogId);


    @Query("select m from BlogMedia as t,Media m where t.mediaId=m.id and t.state=0 and t.blogId=:blogId")
    List<Media> findMediaByBlogId(@Param("blogId") Long blogId);

    @Modifying
    @Query("update BlogMedia set state=1 where blogId=:blogId")
    int updateStateByBlogId(@Param("blogId") Long blogId);



}
